/**
 *@Copyright:Copyright (c) 2015 - 2100
 *@Company:WSN
 */
package com.wwg.component.serial_num;

import com.wwg.aspect.DistLock;
import com.wwg.common.FinanceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 *类描述:序号，编号生成服务实现类
 *
 *@Author:黄婷
 *@date:2019年9月3日
 *@Version:1.0
 */
@Service("serialNumberService")
public class SerialNumberServiceImpl implements ISerialNumberService {

	private static Logger logger = LoggerFactory.getLogger(SerialNumberServiceImpl.class);

	private static final long EFFECTIVE_TIME = 86400000L;

	@Autowired
	private RedisTemplate redisTemplate;

	@Override
	@DistLock(synKey = "approveAttachmentSeqNo", sleepMills = 1000, isWait = true)
	public String getApproveAttachmentSeqNo(Date currentDate) throws FinanceException {
		try {
			String seqNoKey = StringUtil.wsnReplace("approveAttachmentSeqNo_{}_key",
					DateUtil.convert2String(currentDate, DateUtil.FORMATE_YYYYMMDD));
			Object redisModel = redisTemplate.opsForValue().get(seqNoKey);
			if (redisModel != null) {
				//logger.debug("getTrackSeqNo.redisModel={}",redisModel.toString());
				redisTemplate.opsForValue().set(seqNoKey, String.valueOf(Long.valueOf(redisModel.toString()) + 1), EFFECTIVE_TIME, TimeUnit.MILLISECONDS);
			} else {
				redisTemplate.opsForValue().set(seqNoKey, "1", EFFECTIVE_TIME, TimeUnit.MILLISECONDS);
			}
			String trackSeqNo = (String) redisTemplate.opsForValue().get(seqNoKey);
			//logger.debug("getTrackSeqNo.trackSeqNo={}",trackSeqNo);
			return trackSeqNo;
		} catch (Exception e) {
			throw new FinanceException(e.getMessage(), e);
		}

	}

//	@SuppressWarnings("unchecked")
//	@Override
//	@DistLock(synKey = "WDH", sleepMills = 1000, isWait = true)
//	public String getWdhSeqNo(Date currentDate) throws FinanceException {
//		try {
//			String seqNoKey = StringUtil.wsnReplace("wdhSeqNo_{}_key",
//					DateUtil.convert2String(currentDate, DateUtil.FORMATE_YYYYMMDD));
//			Object redisModel = redisTemplate.opsForValue().get(seqNoKey);
//			if (redisModel != null) {
//				redisTemplate.opsForValue().set(seqNoKey, String.valueOf(Long.valueOf(redisModel.toString()) + 1), EFFECTIVE_TIME, TimeUnit.MILLISECONDS);
//			} else {
//				redisTemplate.opsForValue().set(seqNoKey, "1", EFFECTIVE_TIME, TimeUnit.MILLISECONDS);
//			}
//			long num = Long.parseLong((String) redisTemplate.opsForValue().get(seqNoKey)) ;
//			String trackSeqNo = String.format("%04d", num);
//			return StringUtil.wsnReplace("WDH{}{}", DateUtil.convert2String(currentDate, DateUtil.FORMATE_YYMMDD),trackSeqNo);
//		} catch (Exception e) {
//			throw new FinanceException(e.getMessage(), e);
//		}
//	}
//
//	@SuppressWarnings("unchecked")
//	@Override
//	@DistLock(synKey = "YPJ", sleepMills = 1000, isWait = true)
//	public String getYpjSeqNo(Date currentDate) throws FinanceException {
//		try {
//			String seqNoKey = StringUtil.wsnReplace("YpjSeqNo_{}_key",
//					DateUtil.convert2String(currentDate, DateUtil.FORMATE_YYYYMMDD));
//			Object redisModel = redisTemplate.opsForValue().get(seqNoKey);
//			if (redisModel != null) {
//				redisTemplate.opsForValue().set(seqNoKey, String.valueOf(Long.valueOf(redisModel.toString()) + 1), EFFECTIVE_TIME, TimeUnit.MILLISECONDS);
//			} else {
//				redisTemplate.opsForValue().set(seqNoKey, "1", EFFECTIVE_TIME, TimeUnit.MILLISECONDS);
//			}
//			long num = Long.parseLong((String) redisTemplate.opsForValue().get(seqNoKey)) ;
//			String trackSeqNo = String.format("%04d", num);
//			return StringUtil.wsnReplace("YPJ{}{}", DateUtil.convert2String(currentDate, DateUtil.FORMATE_YYMMDD),trackSeqNo);
//		} catch (Exception e) {
//			throw new FinanceException(e.getMessage(), e);
//		}
//	}
//
//	/**
//     * 生成商品明细在redis中的KEY
//     * @return
//     * @throws FinanceException
//     * @author yyh
//     * @date 2019年11月29日   上午11:43:08
//     */
//	@SuppressWarnings("unchecked")
//	@Override
//	@DistLock(synKey = "PDK", sleepMills = 1000, isWait = true)
//	public String generateProductDetailKey() throws FinanceException {
//		try {
//			Date currentDate = new Date();
//			String seqNoKey = StringUtil.wsnReplace("pdkSeqNo_{}_key",
//					DateUtil.convert2String(currentDate, DateUtil.FORMATE_YYYYMMDD));
//			Object redisModel = redisTemplate.opsForValue().get(seqNoKey);
//			if (redisModel != null) {
//				redisTemplate.opsForValue().set(seqNoKey, String.valueOf(Long.valueOf(redisModel.toString()) + 1), EFFECTIVE_TIME, TimeUnit.MILLISECONDS);
//			} else {
//				redisTemplate.opsForValue().set(seqNoKey, "1", EFFECTIVE_TIME, TimeUnit.MILLISECONDS);
//			}
//			String num = (String) redisTemplate.opsForValue().get(seqNoKey);
//			return StringUtil.wsnReplace("PDK{}{}", DateUtil.convert2String(currentDate, DateUtil.FORMATE_YYMMDD),num);
//		} catch (Exception e) {
//			throw new FinanceException(e.getMessage(), e);
//		}
//	}

	/**
     * 生成文件名称唯一标识
     * @return
     * @throws FinanceException
     * @author yyh
     * @date 2019年11月29日   上午11:43:08
     */
	@SuppressWarnings("unchecked")
	@Override
	@DistLock(synKey = "FNK", sleepMills = 1000, isWait = true)
	public String generateFileNameKey() throws FinanceException {
		try {
			Date currentDate = new Date();
			String seqNoKey = StringUtil.wsnReplace("fnkSeqNo_{}_key",
					DateUtil.convert2String(currentDate, DateUtil.FORMATE_YYYYMMDD));
			Object redisModel = redisTemplate.opsForValue().get(seqNoKey);
			if (redisModel != null) {
				redisTemplate.opsForValue().set(seqNoKey, String.valueOf(Long.valueOf(redisModel.toString()) + 1), EFFECTIVE_TIME, TimeUnit.MILLISECONDS);
			} else {
				redisTemplate.opsForValue().set(seqNoKey, "1", EFFECTIVE_TIME, TimeUnit.MILLISECONDS);
			}
			String num = (String) redisTemplate.opsForValue().get(seqNoKey);
			return StringUtil.wsnReplace("{}{}", DateUtil.convert2String(currentDate, DateUtil.FORMATE_YYMMDD),num);
		} catch (Exception e) {
			throw new FinanceException(e.getMessage(), e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	@DistLock(synKey = "JY", sleepMills = 1000, isWait = true)
	public String getJYSeqNo(Date currentDate) throws FinanceException {
		try {
			String formatDate= DateUtil.convert2String(currentDate, DateUtil.FORMATE_YYYYMMDD);

			String seqNoKey = StringUtil.wsnReplace("jySeqNo_{}_key",
					formatDate);
			Object redisModel = redisTemplate.opsForValue().get(seqNoKey);
			if (redisModel != null) {
				redisTemplate.opsForValue().set(seqNoKey, String.valueOf(Long.valueOf(redisModel.toString()) + 1), EFFECTIVE_TIME, TimeUnit.MILLISECONDS);
			} else {
				redisTemplate.opsForValue().set(seqNoKey, "1", EFFECTIVE_TIME, TimeUnit.MILLISECONDS);
			}
			redisModel = redisTemplate.opsForValue().get(seqNoKey);
			//【解押单号】生成规则：JY+解押日期（8位）+序号（6位），序号当天从000001递增；
	        DecimalFormat df=new DecimalFormat("000000");//三位序列号
	        return "JY"+formatDate+df.format(Long.valueOf(redisModel.toString()));

		} catch (Exception e) {
			throw new FinanceException(e.getMessage(), e);
		}
	}

}

